package com.example.addressbook;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.content.Intent; 
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.content.Context;
import com.bumptech.glide.Glide;
import org.litepal.crud.DataSupport;
import android.provider.CallLog;
import android.database.Cursor;
import android.content.ContentResolver;
import android.view.LayoutInflater;
import android.net.Uri;
import android.content.Intent; 
import org.litepal.crud.DataSupport;
import android.graphics.Color;

import java.util.*;

public class CallLogAdapter extends RecyclerView.Adapter<CallLogAdapter.ViewHolder>{
	
	private Context context;
	
	static class ViewHolder extends RecyclerView.ViewHolder{
		TextView name,number,data,address,state;
		ImageView imageView;
		
		ViewHolder(View view){
			super(view);
			name = (TextView)view.findViewById(R.id.text_view_name);
			address = (TextView)view.findViewById(R.id.text_view_address);
			data = (TextView)view.findViewById(R.id.text_view_date);
			state = (TextView)view.findViewById(R.id.text_view_state);
			imageView = (ImageView)view.findViewById(R.id.circle_image_view_0);
		}
	}
	
	private List< Map<String, String> > callLogList = new ArrayList<>();

	public void query(List< Map<String, String> > callLog){
		this.callLogList = callLog;
		notifyDataSetChanged();
	}
	
	public CallLogAdapter(Context c,List< Map<String, String> > callLog){
		context = c; 
		this.callLogList = callLog;
	}
	
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
		View view = LayoutInflater.from(parent.getContext())
			.inflate(R.layout.recycler_view_1_item,parent,false);
		final ViewHolder holder = new ViewHolder(view);
		view.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
					int position = holder.getAdapterPosition();
					Map<String,String> map = callLogList.get(position);
					String telNumber = map.get("number");
					Intent intent = new Intent(Intent.ACTION_DIAL);
					intent.setData(Uri.parse("tel:"+telNumber));
					((MainActivity)context).startActivityForResult(intent,3);
					((MainActivity)context).overridePendingTransition(android.R.anim.slide_in_left,
																		android.R.anim.slide_out_right);
			}
		});
		return holder;
	}
	
	@Override
	public void onBindViewHolder(ViewHolder holder,int position){
		Map<String,String> map = callLogList.get(position);
		String name = map.get("name");
		String number = map.get("number");
		String data = map.get("date");
		String d = map.get("duration");
		int duration = Integer.parseInt(d);
		String type = map.get("type");

		/////////////////
		List<People> pl = DataSupport.where("telephone = ?",number).find(People.class);
		if(  pl.size() > 0 ){
			//Object picturePath = pl.get(0).getPicturePath();
			//Glide.with(context).load(picturePath).into(holder.imageView);
			name = pl.get(0).getName();
		}else if( name==null ){
			name = number;
		}
		
		int color = Color.BLACK;
		//holder.number.setText(number);
		holder.data.setText(data);
		if( duration==0 && type.equals("拒接") ){
			holder.imageView.setImageResource(R.drawable.ic_close_grey600_18dp);
			holder.state.setText(type);
			color = Color.RED;
		}else if( duration==0 && type.equals("打出")){
			holder.imageView.setImageResource(R.drawable.ic_call_made_grey600_18dp);
			//Glide.with(context).load(R.drawable.ic_call_made_grey600_18dp).into(holder.imageView);
			holder.state.setText("未接通");
			
		}else if( duration==0 && type.equals("接入")){
			holder.imageView.setImageResource(R.drawable.ic_call_missed_grey600_18dp);
			//Glide.with(context).load(R.drawable.ic_call_missed_grey600_18dp).into(holder.imageView);
			holder.state.setText("未接电话");
			color = Color.RED;
		}else{
			if( duration/60 != 0 )
				holder.state.setText((duration/60)+"分"+(duration%60)+"秒");
			else holder.state.setText((duration%60)+"秒");
			if( type.equals("接入") ){
				holder.imageView.setImageResource(R.drawable.ic_call_received_grey600_18dp);
				//Glide.with(context).load(R.drawable.ic_call_received_grey600_18dp).into(holder.imageView);
			}else if( type.equals("打出")){
				holder.imageView.setImageResource(R.drawable.ic_call_made_grey600_18dp);
				//Glide.with(context).load(R.drawable.ic_call_made_grey600_18dp).into(holder.imageView);
			}
		}
		
		holder.name.setTextColor(color);
		holder.name.setText(name);
		//holder.type.setText(type);
		holder.address.setText("中国");
	}
	
	@Override
	public int getItemCount(){
		return callLogList.size();
	}
	
	public String getFirstTelephone(){
		return callLogList.get(0).get("number");
	}
}